﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;

class MakeRandomArray
{
    //行数组个数不能大于列数组个数,元素数量不可能大于数组数量
    static int[][] cloumArray;//列数组
  
  

    static List<int[]> finalResult;//最终数组

    static int arrayElementNum;
    static int arrayTotalNum;
    /// <summary>
    /// 元素取值范围0-N随机数值
    /// 行数组个数不能大于列数组个数,元素数量不可能大于数组数量
    /// </summary>
    /// <param name="elemetNum">数组元素数量</param>
    /// <param name="arrayNum">数组数量</param>
    /// <returns></returns>
    static public List<int[]> makeMultiArray(int elemetNum,int arrayNum)
    {
        arrayElementNum = elemetNum;
        arrayTotalNum = arrayNum;
        
        finalResult = new List<int[]>();
        cloumArray = new int[elemetNum][];

        makeFirstArray();
        Thread.Sleep(10);
        makeSecondArray();
        Thread.Sleep(10);

        for(int i=0;i<arrayTotalNum;i++)
        {
            int[] arr = new int[arrayElementNum];
            for(int j=0;j<arrayElementNum;j++)
            {
                arr[j] = cloumArray[i][j];
            }
            finalResult.Add(arr);
        }

        return finalResult;
    }

   

    static ArrayList makePoolArray(ArrayList haveNum,int max)
    {
        //过滤已有元素
        ArrayList arr = new ArrayList();
     
        int count = 0;
        string str = string.Empty;
        for (int i = 0; i < max; i++)
        {
          
            
            count = 0;
            str = string.Empty;
            for (int j = 0; j < haveNum.Count; j++)
            {
                str += haveNum[j] + "||";
                if (i == (int) haveNum[j])
                {
                    count++;//如果有一样值,跳过
                   // Debug.Log("相同="+i+" and " + haveNum[j]);
                }

            }
            if (count==0)
            {
                //没有一样的就加入
                //Debug.Log("str=" + str);
                //Debug.Log("没有一样的=" + i );
                arr.Add( i);
            
            }

        }
      
        arr= RandomSort(arr);
        return arr;
    }

    public static ArrayList RandomSort(ArrayList array)
    {
        int len = array.Count;
        System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>();
        ArrayList newlist = new ArrayList();
        System.Random rand = new System.Random();
        int i = 0;
        while (list.Count < len)
        {
            int iter = rand.Next(0, len);
            if (!list.Contains(iter))
            {
                list.Add(iter);
                newlist.Add(array[iter]);
                i++;
            }
        }
        return newlist;
    }





    static private void makeFirstArray()
    {
       
       
        columAdd(0, makeRandomArray(arrayElementNum));
    }

    static private void makeSecondArray()
    {
      
        ArrayList rowTempArray =new ArrayList();//横排临时数组
      
        int length = arrayElementNum ;

        string str = string.Empty;
        for (int i =1; i < arrayTotalNum; i++)
        {
            //第一个数组已经有了,所以i从1开始
            //横起初始一条数组
           
            rowTempArray = new ArrayList();//横排初始
            ArrayList rowTempArray1 = new ArrayList();//只考虑横排的时候用
            str = string.Empty;
            Thread.Sleep(10);
            for (int j = 0; j < arrayElementNum; j++)
            {
                //竖向 得到横排每个元素
                int num;
                ArrayList tempArr = new ArrayList();
               

                str = string.Empty;
                ArrayList tempPoolArr = new ArrayList();
                if (j==0)
                {
                    //第一个元素

                    // Debug.Log("加入第一个元素="+ cloumArray[i - 1][j]);
                    num = cloumArray[i - 1][j];
                    tempArr.Add(num);
                    rowTempArray1.Add(num);
                    str += num + "||";

                }
                else
                {
                    foreach(int r_num in rowTempArray)
                    {
                        //Debug.Log("加入横排元素=" + r_num);
                        tempArr.Add(r_num);
                        str += r_num + "||";
                        rowTempArray1.Add(r_num);
                    }
                    
                  
                  

                }
                for (int z = 0; z < i; z++)
                {
                    //Debug.Log(z +":"+j+"==>"+"加入竖排元素=" + cloumArray[z][j]);

                    num = cloumArray[z][j];
                    tempArr.Add(cloumArray[z][j]);
                    str += cloumArray[z][j] + "||";
                }
                //Debug.Log(j+" 加入元素="+str);
                tempPoolArr = makePoolArray(tempArr,arrayElementNum);
                if(tempPoolArr.Count==0)
                {
                    //如果同时考虑横竖排已经没有备选,就只考虑横排    
                    tempPoolArr = makePoolArray(rowTempArray1, arrayElementNum);
                }
                num = (int)tempPoolArr[0];
                rowTempArray.Add(num);
                // Debug.Log("tempPoolArr count=" + tempPoolArr.Count);
                str = string.Empty;
                for (int z = 0;  z < tempPoolArr.Count; z++)
                {
                    str += tempPoolArr[z] + "||";
                    //Debug.Log("返回 "+z+" =" + tempPoolArr[z]);
                }
               // Debug.Log("待选:" + str);
             
              //  Debug.Log("j 循环="+j);
                Thread.Sleep(10);
            }
           // Debug.Log("i 循环=" + i);
            columAdd(i, rowTempArray);
            //Debug.Log(" columAdd(i, rowTempArray)" );
        }

        for (int i = 0; i < arrayTotalNum; i++)
        {
            str = string.Empty;
            for (int j=0;j<arrayElementNum;j++)
            {
                //str+="("+i+" "+j+")"+ cloumArray[i][j];
                str +=cloumArray[i][j]+"||";
            }
            Debug.Log(str);
        }
        return;
       
    }

  

    static private void columAdd(int rowTarget,ArrayList arr)
    {
        
        cloumArray[rowTarget] = new int[arr.Count];
        string str = string.Empty;
        str += rowTarget + "==>";
        for (int i=0;i<arr.Count; i++)
        {
            cloumArray[rowTarget][i] = (int)arr[i];
            str += arr[i] + "//";
            // Debug.Log("cloumArray " + rowTarget + " =" + arr[i]);
        }

        Debug.Log("cloumArray="+str);
        str = string.Empty;
        for (int i = 0; i < cloumArray[rowTarget].Length; i++)
        {
            cloumArray[rowTarget][i] = (int)arr[i];
            str += cloumArray[rowTarget][i] + "//";
            // Debug.Log("cloumArray " + rowTarget + " =" + arr[i]);
        }
        Debug.Log(rowTarget+" ==cloumArray=" + str);
        // Debug.Log("cloumArray " + rowTarget + " length =" + cloumArray[rowTarget].Length);
    }

    static private ArrayList makeRandomArray(int length)
    {
        //生成一个数组
        ArrayList a = new ArrayList();
        string str = string.Empty;
        
        for (int i = 0; i < length; i++)
        {
            a.Add(i);
           
           

        }
        a = RandomSort(a);
        for (int i = 0; i < length; i++)
        {
            str += a[i] + "||";



        }
        Debug.Log("random Array="+str);
        return a;
    }

}

    

